package org.example.springboot.controller;

import org.example.springboot.common.Result;
import org.example.springboot.entity.Message;
import org.example.springboot.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;

    // 获取用户消息
    @GetMapping("/user/{userId}")
    public Result<?> getMessagesByUserId(@PathVariable Long userId,
                                         @RequestParam(required = false) Integer messageType,
                                         @RequestParam String userRole) {
        return messageService.getMessagesByUserId(userId, messageType, userRole);
    }

    // 获取未读消息数量
    @GetMapping("/unread/count/{userId}")
    public Result<?> getUnreadCount(@PathVariable Long userId,
                                    @RequestParam String userRole) {
        return messageService.getUnreadCount(userId, userRole);
    }

    // 标记消息为已读
    @PutMapping("/read/{id}")
    public Result<?> markAsRead(@PathVariable Long id,
                                @RequestParam Long currentUserId,
                                @RequestParam String userRole) {
        return messageService.markAsRead(id, currentUserId, userRole);
    }

    // 分页获取用户消息
    @GetMapping("/page")
    public Result<?> getMessagesByPage(@RequestParam Long userId,
                                       @RequestParam(required = false) Integer messageType,
                                       @RequestParam Integer currentPage,
                                       @RequestParam Integer size,
                                       @RequestParam String userRole) {
        return messageService.getMessagesByPage(userId, messageType, currentPage, size, userRole);
    }

    // 获取对话详情
    @GetMapping("/conversation/{conversationId}")
    public Result<?> getConversationDetail(@PathVariable Long conversationId,
                                           @RequestParam Long currentUserId,
                                           @RequestParam String userRole) {
        return messageService.getConversationDetail(conversationId, currentUserId, userRole);
    }

    // 获取用户对话列表
    @GetMapping("/conversations/{userId}")
    public Result<?> getUserConversations(@PathVariable Long userId,
                                          @RequestParam String userRole) {
        return messageService.getUserConversations(userId, userRole);
    }

    // 标记整个对话为已读
    @PutMapping("/conversation/read/{conversationId}")
    public Result<?> markConversationAsRead(@PathVariable Long conversationId,
                                            @RequestParam Long userId,
                                            @RequestParam String userRole) {
        return messageService.markConversationAsRead(conversationId, userId, userRole);
    }

    // 发送回复消息
    @PostMapping("/reply")
    public Result<?> sendReply(@RequestBody Message message) {
        return messageService.createMessage(message);
    }

    @PostMapping("/create")
    public Result<?> createMessage(@RequestParam Long senderId,@RequestParam String content,@RequestParam Integer messageType,@RequestParam Long productId) {
        return messageService.createMessageConversation(senderId,content, messageType, productId);
    }
}